<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>分类信息管理</title>
	<link rel="stylesheet" href="${pageContext.request.contextPath}/tool/ztree/css/zTreeStyle.css" type="text/css">
    <link href="${pageContext.request.contextPath}/style/style.css" rel="stylesheet" type="text/css" />
	
	<script language="javascript" type="text/javascript" src="${pageContext.request.contextPath}/tool/jquery/jquery.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/tool/ztree/js/jquery.ztree.core-3.3.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/tool/ztree/js/jquery.ztree.exedit-3.3.js"></script>
	<script language="javascript" src="${pageContext.request.contextPath}/js/public.js"></script>
	<script language="javascript" src="${pageContext.request.contextPath}/js/common.js"></script>
	<SCRIPT type="text/javascript">
		<!--
		var setting = {
			view: {
				addHoverDom: addHoverDom,
				removeHoverDom: removeHoverDom,
				selectedMulti: false
			},
			edit: {
				enable: true,
				editNameSelectAll: true
			},
			async: {
				enable: true,
				url:"/admin/cate/getSubCate.action",
				autoParam:["id", "name=n", "level=lv"],
				otherParam:{"treeType":"2"},
				dataFilter: filter
			},
			callback: {
				beforeClick: beforeClick,
				beforeAsync: beforeAsync,
				onAsyncError: onAsyncError,
				onAsyncSuccess: onAsyncSuccess,
				beforeDrag: beforeDrag,
				beforeEditName: beforeEditName,
				beforeRemove: beforeRemove,
				beforeRename: beforeRename,
				onRemove: onRemove,
				onRename: onRename
			}
		};

		function filter(treeId, parentNode, childNodes) {
			if (!childNodes) return null;
			for (var i=0, l=childNodes.length; i<l; i++) {
				childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
			}
			return childNodes;
		}
		function beforeClick(treeId, treeNode) {
			//if (!treeNode.isParent) {
//				alert("请选择父节点");
//				return false;
//			} else {
//				return true;
//			}
			return true;
		}
		
		var className = "dark";
		function beforeDrag(treeId, treeNodes) {
		//alert("d");
			return false;
		}
		function beforeEditName(treeId, treeNode) {
			className = (className === "dark" ? "":"dark");
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.selectNode(treeNode);
			return true;
		}
		function beforeRemove(treeId, treeNode) {
			className = (className === "dark" ? "":"dark");
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.selectNode(treeNode);
			if(confirm("确认删除 节点 -- " + treeNode.name + " 吗？")){
				if(treeNode.isParent){
					alert("分类下还有子分类，无法删除");
					return false;
				}else{
					return true;
				}
			}
			return false;
		}
		function onRemove(e, treeId, treeNode) {
			//alert("del---"+treeNode.id);
			$.post("/admin/cate/do_remove.action", { id: treeNode.id},
  					function(data){
    					if(data!="success"){
    						alert(data);
    						location.reload();
    					}
  				});
		}
		function beforeRename(treeId, treeNode, newName) {
			className = (className === "dark" ? "":"dark");
			if (newName.length == 0) {
				alert("节点名称不能为空.");
				var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				setTimeout(function(){zTree.editName(treeNode)}, 10);
				return false;
			}
			return true;
		}
		function onRename(e, treeId, treeNode) {
			//alert("name==="+treeNode.name);
			$.post("/admin/cate/do_update_catename.action", { id: treeNode.id,catename:treeNode.name},
  					function(data){
    					if(data!="success")alert(data);
  					});
		}
		function beforeAsync(treeId, treeNode) {
			className = (className === "dark" ? "":"dark");
			return true;
		}
		function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
		}
		function onAsyncSuccess(event, treeId, treeNode, msg) {
		}
		
		function getTime() {
			var now= new Date(),
			h=now.getHours(),
			m=now.getMinutes(),
			s=now.getSeconds(),
			ms=now.getMilliseconds();
			return (h+":"+m+":"+s+ " " +ms);
		}

		function refreshNode(e) {
		//alert("r");
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			type = e.data.type,
			silent = e.data.silent,
			nodes = zTree.getSelectedNodes();
			if (nodes.length == 0) {
				alert("请先选择一个父节点");
			}
			for (var i=0, l=nodes.length; i<l; i++) {
				zTree.reAsyncChildNodes(nodes[i], type, silent);
				if (!silent) zTree.selectNode(nodes[i]);
			}
		}
		function addHoverDom(treeId, treeNode) {
			var sObj = $("#" + treeNode.tId + "_span");
			if (treeNode.editNameFlag || $("#addBtn_"+treeNode.id).length>0) return;
			var addStr = "<span class='button add' id='addBtn_" + treeNode.id
				+ "' title='add node' onfocus='this.blur();'></span>";
			sObj.after(addStr);
			var btn = $("#addBtn_"+treeNode.id);
			if (btn) btn.bind("click", function(){
				var newCateName = prompt("请输入分类名称");
				$.get("/admin/cate/add_cate.action", { pid: treeNode.id, catename: newCateName },
  					function(data){
    					var zTree = $.fn.zTree.getZTreeObj("treeDemo");
						zTree.addNodes(treeNode, {id:data, pId:treeNode.id, name:newCateName});
  				});
				
				return false;
			});
		};
		function removeHoverDom(treeId, treeNode) {
			$("#addBtn_"+treeNode.id).unbind().remove();
		};
		$(document).ready(function(){
			$.fn.zTree.init($("#treeDemo"), setting);
		});
		//-->
	</SCRIPT>
	<style type="text/css">
.ztree li span.button.add {margin-left:2px; margin-right: -1px; background-position:-144px 0; vertical-align:top; *vertical-align:middle}
	</style>
  </head>
<body>
<ul id="treeDemo" class="ztree"></ul>
</body>
</html>


